package org.bite.lesson03;

public class Demo303 {
    /**
     * 线程安全问题示例
     * @Author kiku-wa
     */
    public static void main(String[] args) throws InterruptedException {
        Count count = new Count();
        //用两个线程对同一个线程进行累加
        Thread t1 = new Thread(()->{
            for(int i = 0; i < 50000; i++){
                count.increase();
            }
        });
        Thread t2 = new Thread(()->{
            for(int i = 0; i < 50000; i++){
                count.increase();
            }
        });
        //开始线程
        t1.start();
        t2.start();
        //等待线程执行完成
        t1.join();
        t2.join();
        //打印两个线程累加后的总结果
        System.out.println("count = " + count.count);
    }
}
//专门用来累加的类
class Count{
    public int count = 0;
    public void increase(){
        count++;
    }
}